package org.jpro.common.util;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;

/**
 * <P>功能描述 : 泛型工具类 </P>
 * <P>File name : GenericsUtils </P>
 * <P>Author : 王泽浩 </P>
 * <P>E-Mail : 1028625100@qq.com </P>
 * <P>Date : 2017/1/1 下午8:29</P>
 */
public class GenericsUtils {

    private GenericsUtils() {
    }

    /**
     * 根据SystemControllerLog和SystemServiceLog 获取日志描述
     * @param <T> the type parameter
     * @param joinPoint 切入点
     * @param clazz 需要获取日志的注解
     * @return the annontation by join point
     * @throws ClassNotFoundException 类没有找到
     */
    public static <T> T getAnnontationByJoinPoint(JoinPoint joinPoint, Class clazz) throws ClassNotFoundException {
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
        Class targetClass = Class.forName(targetName);
        Method[] methods = targetClass.getMethods();
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                Class[] clazzs = method.getParameterTypes();
                if (clazzs.length == arguments.length) {
                    return (T) method.getAnnotation(clazz);
                }
            }
        }
        return null;
    }
}
